package com.xueke.crawler.service;

import com.xueke.crawler.dao.DocRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class CheckFileService {

    @Value("${local.rootPath}")
    protected String rootPath;

    @Autowired
    private DocRelationMapper docRelationMapper;

    public void updateInvalidFileStatus(){

        File file = new File(rootPath);
        if(!file.isDirectory()) return ;

        updateDirectory(file);



    }

    private void updateDirectory(File file) {
        File[] files =  file.listFiles();

        for(File f :files){
            if(f.isDirectory()){
                updateDirectory(f);
            }else{
                long fileSize = f.length();
                String fileName = f.getName();
                if(fileSize<3000){
                    if(fileName.contains(".")){
                        fileName = fileName.substring(0,fileName.indexOf("."));
                    }
                    docRelationMapper.resetDownloadStatus(fileName);
                    f.delete();
                    System.out.println(fileName + "----------------" + fileSize);
                }
            }

        }
    }

}
